package com.citrix.gs.kit;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EmailSender {
    
    private static Logger _log = LoggerFactory.getLogger(EmailSender.class);
    
    
    private String host = "sinpmail.citrite.net";
    private String username = "citrite\\svcacct_gsnkg";
    private String password = "7eyuchXx";
    private String mailFrom = "svcacct_gsnkg@citrix.com";
    private String mailSubject = "TDS Notification";
    private String mailPerson = "GSTDS";
    
    public String getMailFrom() {
        return mailFrom;
    }

    public void setMailFrom(String mailFrom) {
        this.mailFrom = mailFrom;
    }

    public String getMailSubject() {
        return mailSubject;
    }

    public void setMailSubject(String mailSubject) {
        this.mailSubject = mailSubject;
    }

    public String getMailPerson() {
        return mailPerson;
    }

    public void setMailPerson(String mailPerson) {
        this.mailPerson = mailPerson;
    }
    
    private EmailSender() {
    }
    
    static class EmailSenderHolder {
        static EmailSender instance = new EmailSender();
    }

    public static EmailSender getInstance() {
        return EmailSenderHolder.instance;
    }
    
    // internal class, used to authentication
    public class EmailAutherticator extends Authenticator {
        public EmailAutherticator() {
            super();
        }

        public EmailAutherticator(String user, String pwd) {
            super();
            username = user;
            password = pwd;
        }

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    }
    public void sendHtmlEmail(String subject, String [] mailTo , String mailBody) {
        
        Properties props = new Properties(); 
        
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.debug", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        
        Authenticator auth = new EmailAutherticator();
        Session session = Session.getDefaultInstance(props, auth);
    }
    
    // send mail without attachment
    public void send(String subject, String[] mailTo, String mailBody) throws Exception {
        
        String mailList = "";
        
        for(String mail : mailTo) {
            mailList += mail + ";";
        }
        
        _log.debug("Send mail with subject: " + subject + "to " + mailList);
        _log.debug("mail body: " + mailBody);
        
        try {
            Properties props = new Properties(); // 获取系统环境
            Authenticator auth = new EmailAutherticator(); // 进行邮件服务器用户认证
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.debug", "true");
            props.put("mail.smtp.auth", "true");
            Session session = Session.getDefaultInstance(props, auth);
            // 设置session,和邮件服务器进行通讯。
            MimeMessage message = new MimeMessage(session);
            // message.setContent("foobar, "application/x-foobar"); // 设置邮件格式
            message.setSubject(subject == null?mailSubject:subject); // 设置邮件主题
            message.setText(mailBody); // 设置邮件正文
            // message.setHeader(mail_head_name, mail_head_value); // 设置邮件标题
            message.setSentDate(new Date()); // 设置邮件发送日期
            Address address = new InternetAddress(mailFrom, mailPerson);
            message.setFrom(address); // 设置邮件发送者的地址
            Address toAddress = null;
            for (int i = 0; i < mailTo.length; i++) {
                toAddress = new InternetAddress(mailTo[i]); // 设置邮件接收方的地址
                message.addRecipient(Message.RecipientType.TO, toAddress);
            }
            toAddress = null;
            Transport.send(message); // 发送邮件
            _log.info("Email send successfully");
        } catch (Exception ex) {
            _log.error("Send mail failed: ", ex);
            throw new Exception(ex.getMessage());
        }
    }
    
    /**
     * @return the host
     */
    public String getHost() {
        return host;
    }

    /**
     * @param host the host to set
     */
    public void setHost(String host) {
        this.host = host;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

}
